package com.erp.app.servlet.web;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.types.ViewableImage;
import com.erp.framework.util.logger.Logger;

public class ViewImageServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Object obj = getFromRequest(request);

            if (obj == null || !(obj instanceof ViewableImage)) {// can't
                                                                    // find the
                                                                    // obj
                                                                    // anywhere!
                Logger.debug(this, "ViewImageServlet: can't find any viewable image object anywhere!");
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                return;
            }

            ViewableImage img = (ViewableImage) obj;
            byte[] bytes = img.getImageByte();

            OutputStream out = response.getOutputStream();
            response.setContentType(img.getImageContentType());
            response.setStatus(HttpServletResponse.SC_OK);

            if (bytes != null)
                out.write(bytes);            

            out.close();
        } catch (Exception e) {
        	Logger.logExceptions(this, e);            
        	response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }

        return;
    }

    private Object getFromRequest(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        String name = request.getParameter("name");
        String property = request.getParameter("property");
        Object obj = null;

        obj = request.getAttribute("name");

        if (obj == null)
            obj = getFromSession(request);

        return obj;
    }

    private Object getFromSession(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        Object obj = null;
        String name = request.getParameter("name");
        String property = request.getParameter("property");

        HttpSession session = request.getSession(false);

        if (session == null)
            return null;

        obj = session.getAttribute(name);

        if (obj == null) {
            obj = getFromApplication(request);
        } else if (obj != null && property != null) {
            obj = PropertyUtils.getProperty(obj, property);
        }

        return obj;
    }

    private Object getFromApplication(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        Object obj = null;
        String name = request.getParameter("name");
        String property = request.getParameter("property");

        ServletContext servletContext = getServletContext();

        if (servletContext == null)
            return null;

        obj = servletContext.getAttribute(name);

        // Since all images that are stored in servlet are application wide
        // if obj is a hashmap, then we can safely assume that the images
        // are saved in a hashmap with their company codes as keys
        if (obj != null && obj instanceof HashMap) {
            HttpSession session = request.getSession(false);

            if (session == null)
                return null;

            return ((HashMap) obj).get(session.getAttribute(ProjectConstants.SESSION_COMPANY_CODE));
        }

        if (obj != null && property != null) {
            obj = PropertyUtils.getProperty(obj, property);
        }

        return obj;
    }
}
